﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Room
{
    public int id;
    public int maxPlayer = 5;
    public Dictionary<string, bool> playerIds = new Dictionary<string, bool>();
    public string ownerId;
    public enum Status
    {
        PREPARE=0,FIGHT=1,
    }
    public Status status;

    public bool AddPlayer(string id)
    {
        Player player = PlayerManager.GetPlayer(id);
        if (player == null)
        {
            Console.WriteLine("玩家为空，添加失败");
            return false;
        }
        if (playerIds.Count>=maxPlayer)
        {
            Console.WriteLine("玩家已满，添加失败");
            return false;
        }
        if (status != Status.PREPARE)
        {
            Console.WriteLine("没准备，添加失败");
            return false;
        }
        if (playerIds.ContainsKey(id))
        {
            Console.WriteLine("已经在房间中，添加失败");
            return false;
        }
        playerIds[id] = true;
        player.camp = SwitchCamp();
        player.roomId = this.id;
        if (ownerId == "")
        {
            ownerId = player.id;
        }
        Broadcast(ToMsg());
        return true;
    }

    private int SwitchCamp()
    {
        int count1 = 0;
        int count2 = 0;
        foreach (string id in playerIds.Keys)
        {
            Player player = PlayerManager.GetPlayer(id);
            if (player.camp == 1)
            {
                count1++;
            }
            if (player.camp == 2)
            {
                count2++;
            }
        }
        if (count1 <= count2)
        {
            return 1;
        }
        else
        {
            return 2;
        }
    }
    public bool RemovePlayer(string id)
    {
        Player player = PlayerManager.GetPlayer(id);
        if(player == null)
        {
            Console.WriteLine("玩家为空，移除失败");
            return false;
        }
        if (!playerIds.ContainsKey(id))
        {
            Console.WriteLine("没有在房间里，移除失败");
            return false;
        }
        playerIds.Remove(id);
        player.camp = 0;
        player.roomId = -1;
        if (isOwner(player))
        {
            ownerId = SwitchOwner();
        }
        if (playerIds.Count == 0)
        {
            RoomManager.RemoveRoom(this.id);
        }
        Broadcast(ToMsg());
        return true;
    }

    public MsgBase ToMsg()
    {
        MsgGetRoomInfo msg = new();
        int count = playerIds.Count;
        msg.players = new PlayerInfo[count];

        int i = 0;
        foreach(string id in playerIds.Keys)
        {
            Player player = PlayerManager.GetPlayer(id);
            PlayerInfo playerInfo = new();
            playerInfo.id = player.id;
            playerInfo.camp = player.camp;
            playerInfo.win = player.data.win;
            playerInfo.lost = player.data.lost;
            playerInfo.isOwner = 0;
            if(isOwner(player))
            {
                playerInfo.isOwner = 1;
            }
            msg.players[i] = playerInfo;
            i++;
        }
        return msg;
    }

    private void Broadcast(MsgBase msg)
    {
        foreach(string id in playerIds.Keys)
        {
            Player player=PlayerManager.GetPlayer(id);
            player.Send(msg);
        }
    }

    private string SwitchOwner()
    {
        foreach(string id in playerIds.Keys)
        {
            return id;
        }
        return "";
    }

    private bool isOwner(Player player)
    {
        return player.id == ownerId;
    }
}
